package com.asp;

/*
  author:          ndg
  date:            2024/3/29
 */

import com.asp.utils.SensitiveWordUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SensitiveWordChecker {

    private final Pattern pattern;

    public SensitiveWordChecker(List<String> sensitiveWords) {
        StringBuilder sb = new StringBuilder();
        for (String word : sensitiveWords) {
            // 使用\b来匹配单词边界，防止部分匹配
            sb.append("\\b").append(Pattern.quote(word)).append("\\b|");
        }
        // 去掉最后的"|"字符
        String regex = sb.substring(0, sb.length() - 1);
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    }

    public boolean containsSensitiveWord(String text) {
        Matcher matcher = pattern.matcher(text);
        return matcher.find();  // 如果有匹配项，返回true；否则返回false
    }

    public static void main(String[] args) {
        String text = "这是一段包含原神的文本。";
        boolean containsSensitiveWord = SensitiveWordUtils.containsSensitiveWord(text);
        System.out.println("文本中是否包含敏感词: " + containsSensitiveWord);  // 输出：文本中是否包含敏感词: true
    }
}
